package com.jcoinexchage.user.controller.web;

import com.jcoinexchage.user.business.VerifyCodeService;
import com.jcoinexchage.user.common.enums.CodeSceneEnum;
import com.jcoinexchage.user.common.enums.ResultCodeEnum;
import com.jcoinexchage.user.common.vo.RegisterEmailReq;
import com.jcoinexchage.user.common.vo.RegisterTelReq;
import com.jcoinexchage.user.repository.dao.MemberDao;
import com.jcoinexchage.user.repository.model.Member;
import com.jcoinexchange.web.annotation.LoginInNeeded;
import com.jcoinexchange.web.entity.BasicResponse;
import com.jcoinexchange.web.util.PasswordUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Tag(name = "授权")
@RestController
@RequestMapping("/registry")
public class RegisterController {

    @Resource
    private VerifyCodeService codeService;

    @Resource
    private MemberDao memberDao;

    @Operation(summary = "手机号注册")
    @PostMapping("/phone")
    @LoginInNeeded
    public BasicResponse<?> registerByPhone(@RequestBody @Valid RegisterTelReq req) {
        // TODO: 2025/2/20 迁移到 Business 层
        String tel = req.getTel();
        boolean verify = codeService.verify(tel, CodeSceneEnum.REGISTER, req.getCode());
        if (!verify) {
            return BasicResponse.error(ResultCodeEnum.USER_CODE_ERR);
        }
        Member byTel = memberDao.queryByPhone(tel);
        if (byTel != null) {
            return BasicResponse.error(ResultCodeEnum.USER_REGISTER_TEL_EXISTS);
        }
        Member member = new Member();
        member.setTel(tel);
        member.setTelAreaCode(req.getAreaCode());
        member.setPwd(PasswordUtils.memberPwd(req.getPassword()));
        member.setCreateTime(new Date());
        member.setModifiedTime(new Date());
        memberDao.insert(member);
        return BasicResponse.success();
    }

    @Operation(summary = "邮箱注册")
    @PostMapping("/email")
    public BasicResponse<?> registerByEmail(@RequestBody @Validated RegisterEmailReq req) {
        String email = req.getEmail();
        boolean verify = codeService.verify(email, CodeSceneEnum.REGISTER, req.getCode());
        if (!verify) {
            return BasicResponse.error(ResultCodeEnum.USER_CODE_ERR);
        }

        Member byTel = memberDao.queryByEmail(email);
        if (byTel != null) {
            return BasicResponse.error(ResultCodeEnum.USER_REGISTER_EMAIL_EXISTS);
        }

        Member member = new Member();
        member.setEmail(email);
        member.setTelAreaCode(null);
        member.setPwd(PasswordUtils.memberPwd(req.getPassword()));

        member.setCreateTime(new Date());
        member.setModifiedTime(new Date());
        memberDao.insert(member);

        return BasicResponse.success();
    }

}
